package com.cskaoyan.controller;

import com.cskaoyan.aspect.OperationLog;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.admin.*;
import com.cskaoyan.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping("/admin/admin")
@RestController
public class AdminSuperviseController {

    @Autowired
    AdminService adminService;

    @RequestMapping("list")
    public BaseRespVo list(Integer page,Integer limit,String username,String sort,String order) {
        AdminListBean adminListBean = adminService.list(page,limit,username,sort,order);
        return BaseRespVo.ok(adminListBean);
    }

    @OperationLog
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody Admin admin) {
        AdminUpdateVO adminCreateVOAndUpdateBVo = adminService.create(admin);
        if (adminCreateVOAndUpdateBVo.getUsername() == null) {
            return BaseRespVo.fail(602,"管理员已经存在");
        }
        String msg = adminCreateVOAndUpdateBVo.getUsername();
        return BaseRespVo.ok(adminCreateVOAndUpdateBVo, msg);
    }

    @OperationLog
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Admin admin) {

        AdminUpdateVO adminUpdateVO =  adminService.update(admin);
        if (adminUpdateVO.getUsername() == null) {
            return BaseRespVo.fail(602,"管理员不存在");
        }
        String msg = adminUpdateVO.getUsername();
        return BaseRespVo.ok(adminUpdateVO, msg);
    }

    @OperationLog
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Admin admin) {
        String username = admin.getUsername();
        adminService.delete(admin);
        return BaseRespVo.ok("删除管理员成功",username);
    }
}
